package com.star.eastern.blee

import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.widget.TextView
import com.star.eastern.blee.utils.*
import com.star.eastern.blee.utils.Tripple7BluetoothCommandProtocol.Companion.INPUT_UUID
import com.star.eastern.blee.utils.Tripple7BluetoothCommandProtocol.Companion.OUTPUT_UUID
import com.tangula.android.utils.ActivityUtils
import com.tangula.android.utils.TaskUtils
import kotlinx.android.synthetic.main.activity_test.*
import java.lang.Exception
import java.util.*

class TestActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)

        VW_HEX_SWICTH.isChecked = HEX_MODE

        VW_IO_SWICTH.isChecked = UUID_INPUT == UUID.fromString(INPUT_UUID)

        VW_LIST_ALL_SWITCH.isChecked = LIST_ALL_DEVICE

        VW_HEX_SWICTH.setOnCheckedChangeListener { _, isChecked ->
            HEX_MODE = isChecked
            //Log.v("console", "HEX_MODE:$HEX_MODE")
        }

        VW_LIST_ALL_SWITCH.setOnCheckedChangeListener { _, isChecked ->
            LIST_ALL_DEVICE = isChecked
            //Log.v("console", "LIST_ALL_DEVICE:$LIST_ALL_DEVICE")
        }


        VW_IO_SWICTH.setOnCheckedChangeListener { _, isChecked ->
            if(isChecked) {
                UUID_INPUT = UUID.fromString(INPUT_UUID)
                UUID_OUTPUT = UUID.fromString(OUTPUT_UUID)
            }else{
                UUID_INPUT = UUID.fromString(OUTPUT_UUID)
                UUID_OUTPUT = UUID.fromString(INPUT_UUID)
            }

            //LogConsole.writeBizLog("SWITH INPUT $UUID_INPUT OUTPUT $UUID_OUTPUT")
        }

        vw_cmd_wait_time_text.setOnEditorActionListener(object:TextView.OnEditorActionListener{
            override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
                v?.also{
                    try {
                        val wt = StringBuffer(it.text).toString().toLong()
                        //LogConsole.writeBizLog("change cmd wait time as $wt")
                        CommandCenter.DEFAULT_WAIT_TIME = wt
                    }catch(ex:Exception){
                        //IGNORE
                    }
                }
                return true
            }
        })




        VW_START.setOnClickListener{ it ->
            startActivity(Intent(this,MainActivity::class.java).also {self->
                self.addFlags(FLAG_ACTIVITY_NEW_TASK)
            })
        }

        vw_log_switch.setOnCheckedChangeListener{_, isChecked ->
            LogConsole.viewLogState = isChecked
        }


    }
}
